package com.jeff.transaction

import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

class CreditController {
	CreditService creditService
	private static final Log log = LogFactory.getLog(this)
	

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [creditInstanceList: Credit.list(params), creditInstanceTotal: Credit.count()]
    }

    def create = {
        def creditInstance = new Credit()
        creditInstance.properties = params
        return [creditInstance: creditInstance]
    }

    def save = {
        def creditInstance = new Credit(params)
        if (creditInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'credit.label', default: 'Credit'), creditInstance.id])}"
            redirect(action: "show", id: creditInstance.id)
        }
        else {
            render(view: "create", model: [creditInstance: creditInstance])
        }
    }

    def show = {
        def creditInstance = Credit.get(params.id)
        if (!creditInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'credit.label', default: 'Credit'), params.id])}"
            redirect(action: "list")
        }
        else {
            [creditInstance: creditInstance]
        }
    }

    def edit = {
        def creditInstance = Credit.get(params.id)
        if (!creditInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'credit.label', default: 'Credit'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [creditInstance: creditInstance]
        }
    }

    def update = {
        def creditInstance = Credit.get(params.id)
		if (creditInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (creditInstance.version > version) {
					
					creditInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'credit.label', default: 'Credit')] as Object[], "Another user has updated this Credit while you were editing")
					render(view: "edit", model: [creditInstance: creditInstance])
					return
				}
			}
			creditInstance.properties = params
			if (!creditInstance.hasErrors() && creditInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'credit.label', default: 'Credit'), creditInstance.id])}"
				redirect(action: "show", id: creditInstance.id)
			}
			else {
				render(view: "edit", model: [creditInstance: creditInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'credit.label', default: 'Credit'), params.id])}"
			redirect(action: "list")
		}
    }

    def delete = {
        def creditInstance = Credit.get(params.id)
        if (creditInstance) {
            try {
                creditInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'credit.label', default: 'Credit'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'credit.label', default: 'Credit'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'credit.label', default: 'Credit'), params.id])}"
            redirect(action: "list")
        }
    }

	def call = {
		log.warn("an WARN log has been logged at Credit Controller")
		log.info("an INFO log has been logged at Credit Controller")
		log.debug("an DEBUG log will not be logged at Credit Controller")
		creditService.updateCredit(null, params)
		redirect(action:"show", id: params.id)
	}
	
	def xmlList = {
		creditService.updateCredit(null, params)
//		def ret = {
//			root {
//				for (i in 0..5) {
//					child(id:i,name:"${i}_child") {
//						grand(id:"-${i}")
//					}
//				}
//			}
//		}
//		
//		render (contentType:"text/xml") ret
		
		render (contentType:"text/xml"){
			airports{
		      for(i in 1..5){
		        airport(id:i, iata:"idata_${i}"){
		          city(id:i)
		        }
		      }        
		    }
		}
	}
}
